from errors import error


class LoopQueue(object):
    def __init__(self, max):
        self.head = self.tail = 0
        self.max_size = max
        self.arr = [0] * max

    def is_full(self):
        return (self.tail + 1) % self.max_size == self.head

    def is_empty(self):
        return self.tail == self.head

    def size(self):
        return (self.tail + self.max_size - self.head) % self.max_size

    def push(self, value):
        if self.is_full():
            return error.FError("队列满")
        self.arr[self.tail] = value
        self.tail = (self.tail + 1) % self.max_size
        return None

    def pop(self):
        if self.is_empty():
            return error.FError("队列空"), None
        value = self.arr[self.head]
        self.head = self.head + 1
        return None, value

    def list(self):
        if self.is_empty():
            print("队列空")
            return
        temp = self.head
        for i in range(self.size()):
            print("arr[{}]={}".format(temp, self.arr[temp]))
            temp = (temp + 1) % self.max_size


if __name__ == '__main__':
    queue = LoopQueue(5)
    flag = True
    while flag:
        print("输入push入队")
        print("输入pop出队")
        print("输入list循环")
        print("输入exit退出")
        key = input()
        if key == "push":
            value = input("请输入：")
            err = queue.push(value)
            if err:
                print(err.message)
            else:
                print("添加成功")
        elif key == "pop":
            err, value = queue.pop()
            if err:
                print(err.message)
            else:
                print(value)
        elif key == "list":
            queue.list()
        elif key == "exit":
            flag = False
        else:
            print("请输入正确的指令")
